import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
import 'package:flutter_net/dataUtil.dart';
import 'package:flutter_net/dialogutil.dart';
import 'package:flutter_net/loadingdialog.dart';
import 'package:flutter_net/netutil.dart';
import 'package:flutter_net/toast.dart';
import 'package:flutter_net/weather.dart';

void main() {
  runApp(MaterialApp(
    home: MyApp(),
    title: '网络请求',
  ));
}

class MyApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return MyAppState();
  }
}

class MyAppState extends State<MyApp> {
  final String url = 'http://t.weather.sojson.com/api/weather/city/101030100';
  var city;
  var time;
  var wendu;
  var pm25;
  var pm10;
  var ganmao;

  _getData() async {
    var loading = LoadingDialog(
      text: '获取中...',
    );
    showDialog(
        context: context,
        barrierDismissible: false,
        builder: (BuildContext context) {
          return loading;
        });
    var result = await HttpUtils.request(
      url,
      method: HttpUtils.GET,
    );
    var weather = Weather.fromJson(result);

    //刷新数据
    setState(() {
      if (weather != null) {
        this.time = weather.time;
        if (weather.cityInfo != null) {
          this.city = weather.cityInfo.city;
        }
        if (weather.data != null) {
          this.wendu = weather.data.wendu;
          this.pm25 = weather.data.pm25;
          this.pm10 = weather.data.pm10;
          this.ganmao = weather.data.ganmao;
        }
      }
      //关闭圆形进度条
      Navigator.pop(context);
    });
  }

  @override
  void initState() {
    super.initState();
    //执行网络请求
    //_getData();
  }

  @override
  void dispose() {
    //页面销毁时
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          '获取网络天气',
        ),
      ),
      body: new Container(
        child: Column(
          children: <Widget>[
            RaisedButton(
              onPressed: () {
                print('执行请求获取网络数据');
                _getData();
              },
              child: Text(
                '获取',
                style: TextStyle(color: Colors.red),
              ),
            ),
            RaisedButton(
              onPressed: () {
                DialogUtil().showAlertDialog(context, '提示', '删除').then((val) {
                  print(val);
                });
              },
              child: Text(
                'AlertDialog',
                style: TextStyle(color: Colors.red),
              ),
            ),
            RaisedButton(
              onPressed: () {
                Toast.toast(context, '提示消息');
              },
              child: Text(
                'Toast',
                style: TextStyle(color: Colors.red),
              ),
            ),
            Container(
                child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Text('城市：${DataUtil.setData(city)}'),
                Text('时间：${DataUtil.setData(time)}'),
                Text('温度：${DataUtil.setData(wendu)}'),
                Text('PM2.5:${DataUtil.setData(pm25)}'),
                Text('PM10:${DataUtil.setData(pm10)}'),
                Text('感冒指数:${DataUtil.setData(ganmao)}'),
              ],
            ))
          ],
        ),
      ),
    );
  }
}
